#!/bin/bash

basedir=/home/z/build/1packages/virtualbox/
source "${basedir}/PKGBUILD"

srcdir="${basedir}/makepkg_pacman/virtualbox/src/VirtualBox-${pkgver}/"
moddir="${srcdir}/out/linux.amd64/release/bin/src/"

set -e

cd "$moddir"

kerneldate="`date --date="$(uname -v|cut -f4- -d' ')" +%s`"
moduledate="`stat --format=%Y -- ./vboxdrv.ko`"
if test "$kerneldate" -ge "$moduledate"; then
  #XXX recompile virtualbox host modules only if newer kernel than the last time modules were compiled!
  make
fi

set +e

if ! lsmod|grep -i vbox >/dev/null; then
 sudo insmod -- ./vboxdrv.ko
 sudo insmod -- ./vboxnetadp.ko
 sudo insmod -- ./vboxnetflt.ko
 sudo insmod -- ./vboxpci.ko
 sudo -K
fi


/usr/bin/VirtualBox
ec="$?"
echo "exit code: $ec"

if lsmod|grep -i vbox >/dev/null; then
  #if it's not still running
  if ! ps axuw|grep VirtualBox|grep -v grep >/dev/null; then
    sudo rmmod -- vboxnetadp vboxnetflt vboxpci vboxdrv
    #sudo rmmod -- /home/z/build/1packages/virtualbox/makepkg_pacman/virtualbox/src/VirtualBox-5.1.6/out/linux.amd64/release/bin/src/vboxnetadp.ko /home/z/build/1packages/virtualbox/makepkg_pacman/virtualbox/src/VirtualBox-5.1.6/out/linux.amd64/release/bin/src/vboxnetflt.ko /home/z/build/1packages/virtualbox/makepkg_pacman/virtualbox/src/VirtualBox-5.1.6/out/linux.amd64/release/bin/src/vboxpci.ko /home/z/build/1packages/virtualbox/makepkg_pacman/virtualbox/src/VirtualBox-5.1.6/out/linux.amd64/release/bin/src/vboxdrv.ko
    sudo -K
  fi
fi

exit $ec

